package com.chunfen.dubbo.protocol.dubbo;

import com.chunfen.dubbo.framework.Invocation;
import com.chunfen.dubbo.provider.LocalRegister;
import io.netty.channel.ChannelHandlerContext;
import io.netty.channel.ChannelInboundHandlerAdapter;

import java.io.Serializable;
import java.lang.reflect.Method;
import java.util.Map;

public class NettyServerHandler extends ChannelInboundHandlerAdapter {
    @Override
    public void channelRead(ChannelHandlerContext ctx, Object msg) throws Exception {

        Invocation invocation = (Invocation) msg;

        Class implClass = LocalRegister.get(invocation.getInterfaceName());

        Method method = implClass.getMethod(invocation.getMethodName(), invocation.getParamTypes());

        Map<String, ? extends Serializable> result = (Map<String, ? extends Serializable>) method.invoke(implClass.newInstance(), invocation.getParams());

        ctx.writeAndFlush(result);
    }

}
